import Storage from 'storage';

import SystemUtil from 'utils/systemUtil';

export default class HistoryUtil {
  static getCurrentUrl(){
    let location = window.location;
    let url = location.pathname;
    let searchData = location.search;
    if (!SystemUtil.isEmpty(searchData)) {
      url += searchData;
    }
    return url;
  }

  static add(url) {
    let historyList = Storage.getSession('urlHistory');
    if (SystemUtil.isEmpty(historyList)) {
      historyList = [];
    }
    historyList.push(url);
    Storage.setSession('urlHistory', historyList);
  }

  static delete() {
    let historyList = Storage.getSession('urlHistory');
    if (SystemUtil.isEmpty(historyList)) {
      return;
    }

    if (historyList.length === 1) {
      this.clear();
      return;
    }

    historyList.splice(historyList.length - 1, 1);
    Storage.setSession('urlHistory', historyList);
  }

  static getUrl() {
    let historyList = Storage.getSession('urlHistory');
    if (SystemUtil.isEmpty(historyList)) {
      return null;
    }

    let url = historyList[historyList.length - 1];
    return url;
  }

  static clear() {
    Storage.removeSession('urlHistory');
  }
}
